package cn.edu.scau.cmi.liangzaoqing.fxml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.commons.io.FileUtils;
import org.apache.metamodel.schema.Column;
import org.apache.metamodel.schema.Schema;
import org.apache.metamodel.schema.Table;

import cn.edu.scau.cmi.liangzaoqing.database.model.ColumnType;
import cn.edu.scau.cmi.liangzaoqing.database.model.DataBaseType;
import cn.edu.scau.cmi.liangzaoqing.database.model.ModelType;
import cn.edu.scau.cmi.liangzaoqing.database.model.ObjectFactory;
import cn.edu.scau.cmi.liangzaoqing.database.model.PackageType;
import cn.edu.scau.cmi.liangzaoqing.database.model.PhysicalPrimaryKeysType;
import cn.edu.scau.cmi.liangzaoqing.database.model.Project;
import cn.edu.scau.cmi.liangzaoqing.database.model.TableType;
import cn.edu.scau.cmi.liangzaoqing.database.model.TablesType;
import cn.edu.scau.cmi.liangzaoqing.domain.LeafNode;
import cn.edu.scau.cmi.liangzaoqing.domain.MiddleNode;
import cn.edu.scau.cmi.liangzaoqing.domain.RootNode;
import cn.edu.scau.cmi.liangzaoqing.util.DatabaseMetaDataOutputUtil;
import cn.edu.scau.cmi.liangzaoqing.util.XmlToBeanUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.Label;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FileTreeController implements Initializable {

	private String sourceProjectPath;
	
	private String distinationProjectPath;
	
	@FXML
	private Label projectLabel;
	
	@FXML
	private Button beginCopyButton;
	
	@FXML
	private Button checkFileButton;
	
	@FXML
	private Button closeButton;
	
	@FXML
	private TreeView treeView;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	}
	
	public void setSourceProjectPath(String projectPath) {
		this.sourceProjectPath = projectPath;
		projectLabel.setText(projectPath.substring(projectPath.lastIndexOf("\\")+1));
	}
	
	public void setDistinationProjectPath(String projectPath) {
		this.distinationProjectPath = projectPath;
		//System.out.println(projectPath);
	}
	
	public void iterateDirectory(File directory,CheckBoxTreeItem<String> root) {
		if(directory.isDirectory()) {
			for(File file:directory.listFiles()) {
				CheckBoxTreeItem<String> child = new CheckBoxTreeItem<>();
				child.setValue(file.getName());
				root.getChildren().add(child);
				if(file.isDirectory()) {
					iterateDirectory(file,child);
				}
			}
			return;
		}
	}
	
	@SuppressWarnings("unchecked")
	public void initTreeView(){
		if(sourceProjectPath!="") {
			String projectName = sourceProjectPath.substring(sourceProjectPath.lastIndexOf("\\")+1);
			//System.out.println("projectName:"+projectName);
			CheckBoxTreeItem<String> root = new CheckBoxTreeItem<>();
			root.setExpanded(true);
			root.setValue(projectName);
			treeView.setCellFactory(CheckBoxTreeCell.forTreeView());
			treeView.setRoot(root);
			File projectFile = new File(sourceProjectPath);
			iterateDirectory(projectFile,root);
		}
	}

	public void beginCopy() throws IOException {
		CheckBoxTreeItem<String> project = (CheckBoxTreeItem<String>)treeView.getRoot();
		iterateCopyFile("",project);
		Stage nextStage = new Stage();
		nextStage.setTitle("文件复制应用");
		FXMLLoader loader = new FXMLLoader(getClass().getResource("CopyFileSuccessPage.fxml"));
		Pane nextPane = loader.load();
		CopyFileSuccessController copyFileSuccessController = (CopyFileSuccessController) loader
				.getController();
		copyFileSuccessController.changeLabelContent("文件复制成功！");
		Scene nextScene = new Scene(nextPane);
		nextStage.setScene(nextScene);
		nextStage.show();
	}
	
	public void iterateCopyFile(String rootFilePath,CheckBoxTreeItem<String> root) throws IOException {
		System.out.println(trimFlash(sourceProjectPath+"\\"+rootFilePath));
		File file = new File(trimFlash(sourceProjectPath+"\\"+rootFilePath));
		String path = distinationProjectPath+"\\"+rootFilePath;
		System.out.println("path:>>"+path);
		//获取上一级目录路径
		path=path.length()==0?path:path.substring(0,path.lastIndexOf("\\"));
		if(root.isSelected() && file.isDirectory()) {
			System.out.println(trimFlash("Directory:++>>"+path));
			FileUtils.copyDirectoryToDirectory(file, new File(trimFlash(path)));
			return;
		}else if(root.isSelected() && file.isFile()) {
			System.out.println(trimFlash("File:==>>"+path));
			FileUtils.copyFileToDirectory(file, new File(trimFlash(path)));
			return;
		}else if(!root.isSelected() && file.isFile()) {
			//
		}else{//!root.isSelected() && file.isDirectory()
			CheckBoxTreeItem<String> child = new CheckBoxTreeItem<String>();
			for (int i = 0; i < root.getChildren().size(); i++) {
				child = (CheckBoxTreeItem<String>) root.getChildren().get(i);
				iterateCopyFile(trimFlash(rootFilePath+"\\"+child.getValue()),child);
			}
			return;
		}
	}
	
	public String trimFlash(String stringWithFlash) {
		if(stringWithFlash.startsWith("\\")) {
			stringWithFlash = stringWithFlash.substring(1);
		}
		if(stringWithFlash.endsWith("\\")) {
			stringWithFlash = stringWithFlash.substring(0,stringWithFlash.length()-1);
		}
		return stringWithFlash;
	}
	
	public void checkFile() {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("打开目标文件夹");
//		String path = System.getProperty("user.dir");
//		path = path.substring(0, path.lastIndexOf("\\"));
//		System.out.println(path);
//		File defaultDirectory = new File(path);
		fileChooser.setInitialDirectory(new File(distinationProjectPath));
//		fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.xml"));
		fileChooser.showOpenMultipleDialog(checkFileButton.getScene().getWindow());
	}
	
	public void closeStage() {
		Stage stage = (Stage)closeButton.getScene().getWindow();
		stage.close();
	}
}
